package com.spring.test.instance.service;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;


/**
 * @author liyanhao
 * @Date 2023-03-03 11:33
 */
@Component
public class ZhangSanInstantiationAwareBeanPostProcessor
        implements InstantiationAwareBeanPostProcessor {
    @Override
    public PropertyValues postProcessProperties(
            PropertyValues pvs, Object bean, String beanName) throws BeansException {
        if ("userService".equals(beanName)) {
            for (Field field : bean.getClass().getFields()) {
                if(field.isAnnotationPresent(IntINJect.class)){
                    field.setAccessible(true);
                    try {
                        field.set(bean, 123);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return pvs;
    }
}
